using System;
using System.Drawing;
using System.IO;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Ro.Norc.ServiceStackClientUploadAndDownloadProgress.ServiceModel.Operations;
using Ro.Norc.ServiceStackClient;

namespace Ro.Norc.ServiceStackClientUploadAndDownloadProgress
{
	public class HelloViewController : UIViewController
	{
		private UILabel mLbl = null;
		
		private UIProgressView mProgressView = null;
		
		private UIButton mButton = null;
		
		private Service<HelloResponse> mUploadService = null;
		
		public HelloViewController () : base()
		{
			return;	
		}

		private string GetFileData()
		{
			string path = Path.Combine(NSBundle.MainBundle.ResourcePath, "ProtoBuf.ppt");
			string base64Contents = null;
			byte[] contents = null;
			using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
			{
				contents = new byte[fs.Length];
				fs.Read(contents, 0, contents.Length);
				base64Contents = Convert.ToBase64String(contents);
			}
			return base64Contents;
		}
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			bool bFirstDownloadProgress = true;
			bool bFirstUploadProgress = true;
			
			//configure service
			mUploadService = new Service<HelloResponse>("http://tests.servicestack.hello/ws");
			mUploadService.Encoder = new ServiceEncoderJson();
			mUploadService.ServiceResult += delegate(object sender, ServiceResultArgs<HelloResponse> args) {
				bFirstDownloadProgress = bFirstUploadProgress = true;
			};
			mUploadService.ServiceError += delegate {
				bFirstDownloadProgress = bFirstUploadProgress = true;
			};
			mUploadService.ServiceProgress += delegate(object sender, ServiceProgressArgs args) {
				if (bFirstDownloadProgress) {
					bFirstDownloadProgress = false;
					mProgressView.SetProgress(0, false);
				}
				mProgressView.SetProgress((float)args.Progress, true);
				mLbl.Text = string.Format("Download progress: {0}%", args.Progress * 100);
			};
			mUploadService.ServiceUploadProgress += delegate(object sender, ServiceProgressArgs args) {
				if (bFirstUploadProgress) {
					bFirstUploadProgress = false;
					mProgressView.SetProgress(0, false);
				}
				mProgressView.SetProgress((float)args.Progress, true);
				mLbl.Text = string.Format("Upload progress: {0}%", args.Progress * 100);
			};
			
			//configure button
			mButton = UIButton.FromType(UIButtonType.RoundedRect);
			mButton.Frame = new RectangleF(View.Frame.Width / 2 - 150, View.Frame.Height / 2 - 15, 300, 30);
			mButton.SetTitle("Do upload", UIControlState.Normal);
			mButton.TouchDown += delegate {
				mUploadService.Send(new Hello() {
					Name = GetFileData()
				});
			};
			View.AddSubview(mButton);
			
			//configure progress bar
			mProgressView = new UIProgressView();
			mProgressView.Frame = new RectangleF(mButton.Frame.X, mButton.Frame.Y + 35, 300, 20);
			View.AddSubview(mProgressView);
			
			//configure label
			mLbl = new UILabel();
			mLbl.TextColor = UIColor.Black;
			mLbl.Font = UIFont.BoldSystemFontOfSize(13);
			mLbl.Frame = new RectangleF(mProgressView.Frame.X, mProgressView.Frame.Y + 25, 300, 20);
			mLbl.TextAlignment = UITextAlignment.Center;
			View.AddSubview(mLbl);
			
			View.BackgroundColor = UIColor.White;
		}
	}
}